package com.dy.nygl.service.impl;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dy.nygl.mapper.CostAnalysisMapper;
import com.dy.nygl.mapper.MachineMapper;
import com.dy.nygl.pojo.Machine;
import com.dy.nygl.service.MachineService;
import com.dy.nygl.service.ReportSummaryService;

/**
 * 本类 反映能耗使用情况
 * selectMachine ： 将用户输入的查询类型用时间段的方式展示。
 * selectTaskName ：输入任务名查询所用资源的能耗情况。
 * timeAdd： 用于将一定时间段内的数据累加。
 * year、month、day：用于生成时间刻度，用于数据查询。
 * timeSecrionDay、Month、Year ：
 * 本类用于数据查找一般不存储。
 * @author xiao
 */
@Service("machineService")
public class MachineServiceImpl implements MachineService{
	
	@Resource(name="machineDao")
	private MachineMapper machineDao;
	
	@Resource(name="costAnalysisDao")
	private CostAnalysisMapper costAnalysisDao;
	
	@Resource(name="reportSummaryService")
	private ReportSummaryService reportSummaryService;
	
	private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	// 返回按照能源类型排序的总量;输入格式为2016-12-12;
	public List<Double> costAndMachineTime(String time) throws ParseException{
		List<Double> all = new ArrayList<>();
		List<Double> price = costAnalysisDao.selectPrice();
		List<String> listCode = costAnalysisDao.selectCodeMapper();
		for(int i=0; i<listCode.size(); i++){
			all.add(timeAdd(machineDao.selectTimeByBlurry(time, listCode.get(i)))* price.get(i));
		}
		System.out.println("all="+all);
		return all;
	}
	
	public List<Double> costPriceByEnerty(List<String> machine, Double code) {
		List<Double> data = new ArrayList<>();
		Double sum = 0.0;
		String tempStr = "";
		for(int i = 0 ;i<machine.size();i++) {
			tempStr = machine.get(i).trim();
			sum+=Double.valueOf(tempStr)*code;
			data.add(Double.valueOf(tempStr)*code);
		}
		data.add(sum);
		System.out.println(data);
		return data;
	}
	
	// 返回按照能源类型排序的总量
	public List<Double> costAndMachineTask(String taskName) throws ParseException{
		List<Double> all = new ArrayList<>();
		List<Double> price = costAnalysisDao.selectPrice();
		List<Double> sum = selectTaskName(taskName);
		for(int i = 0; i<sum.size(); i++){
			Double d = sum.get(i)*price.get(i);
			all.add(d);
		}
		System.out.println("返回按照能源类型排序的总量="+all);
		return all;
	}
	
	public List<Double> MachineTask(String taskName) throws ParseException{
		List<Double> all = new ArrayList<>();
		List<Double> price = costAnalysisDao.selectPrice();
		List<Double> sum = selectTaskName(taskName);
		for(int i = 0; i<sum.size(); i++){
			Double d = sum.get(i)*price.get(i);
			all.add(d);
		}
		System.out.println("返回按照能源类型排序的总量="+all);
		return all;
	}
	
	// 获取以能源排列的数据
	public List<Double> randomTime(String startTime,String endTime){
		List<String> listCode = costAnalysisDao.selectCodeMapper();
		List<Double> all = new ArrayList<>();
		for(int i = 0; i< listCode.size();i++){
			double add = timeAdd(machineDao.selectRandomTime(startTime, endTime, listCode.get(i)));
			all.add(add);
		}
		return all;
	}
	
	/** 采集类型分类--> 提供时间段和监控资源类型
	 * 传机器名称  --> 不同时间段内的能源总消耗，假设拿到的数据是单位时间刷新的，需要进行累加显示
	 */
	public List<Double> selectMachine(String object,String time) throws ParseException{
		System.out.println("进入能源和机器名的类型查找。。。。。。。。。。。。");
		if(time.length()<5){
			return year(object,time);
		}else if(time.length()<8){
			return month(object,time);
		}else if(time.length()<11){
			return day(object,time);
		}
		return null;
	}
	
	/** 根据任务名和资源类型查找，获取costAnalysisDao 的能源类型和顺序。
	 */
	public List<Double> selectTaskName(String taskName) throws ParseException{
		List<Double> sum = new ArrayList<>();
		List<String> listCode = costAnalysisDao.selectCodeMapper();
		System.out.println("进行全部统计。。。");
		System.err.println(taskName);
		for(int i = 0;i<listCode.size();i++){
			sum.add(timeAdd(machineDao.selectTaskName(taskName, listCode.get(i))));
		}
		System.out.println("统计完成： "+ sum);
		return sum;
	}

	// 数据累加
	public Double timeAdd(List<Double> data){
		Double sum = 0.0;
		for(int i = 0;i<data.size();i++){
			sum += data.get(i);
		}
		return sum;
	}
	/** 按天分段，6小时
	 * 分别判断输入的是能源类型还是机器名
	 */
	public List<Double> day(String object,String time) throws ParseException{
		List<Date> day = timeSectionDay(time);
		List<Double> data = new ArrayList<>();
		char[] num = object.toCharArray();//把字符串转换为字符数组
		if(Character.isDigit(num[0])){
			for(int i = 0;i<4;i++){
				data.add(timeAdd(machineDao.selectMonitorMedia(object,day.get(i),day.get(i+1))));
				System.out.println(day.get(i)+":"+day.get(i+1));
			}
		}else{
			for(int i = 0;i<4;i++){
				System.out.println("进入天的分段查询。。。。。");
				data.add(timeAdd(machineDao.selectName(object, day.get(i), day.get(i+1))));
				System.out.println(day.get(i)+":"+day.get(i+1));
			}
		}
		System.out.println("day="+data);
		return data;
	}
	public List<String> hcDay(){
		List<String> hcDay = new ArrayList<>();
		for(int i =1;i<5;i++){
			hcDay.add(String.valueOf(i*6));
		}
		return hcDay;
	}
	
	/** 按月（30天）分段,3天，返回时间段数组
	 */
	public List<Double> month(String object,String time) throws ParseException{
		System.out.println("进入month。。。。");
		List<Date> month = timeSecrionMouth(time);
		List<Double> data = new ArrayList<>();
		char[] num = object.toCharArray();
		if(Character.isDigit(num[0])){
			for(int i = 0;i<20;i++){
				if(i%2==0){
					data.add(timeAdd(machineDao.selectMonitorMedia(object,month.get(i),month.get(i+1))));
				}
			}
		}else{
			for(int i = 0;i<20;i++){
				if(i%2==0){
					data.add(timeAdd(machineDao.selectName(object, month.get(i), month.get(i+1))));
					System.out.println(month.get(i)+":"+month.get(i+1));
				}
			}
		}
		System.out.println("month="+data);
		return data;
	}

	public List<Double> monthByOneTime(String startTime) throws ParseException{
		System.out.println("进入monthByOneTime。。。。");
		List<Double> data = new ArrayList<>();
		List<Date> monthAll = timeSecrionMouth(startTime);
		List<Date> month = new ArrayList<>();
		Map<String,Object> map = new LinkedHashMap<>();
		List<String> energyTypeCode = costAnalysisDao.selectEnergyType();
		for(int i = 0; i< monthAll.size();i++){
			if(i%2 != 0){
				month.add(monthAll.get(i));
			}
		}
		System.err.println(month);
		Double s = timeAdd(machineDao.selectMonitorMedia(energyTypeCode.get(1), month.get(0), month.get(1)));
		System.out.println("i="+s);
		return data;
	}
	
	public List<Double> monthBySetTime(String startTime,String endTime) throws ParseException{
		System.out.println("进入monthBySetTime。。。。");
		List<Double> data = new ArrayList<>();
		Date Start = dateFormat.parse(startTime+"-01 00:00:00");
		Date End = dateFormat.parse(endTime+"-30 23:59:59");
		List<String> energyTypeCode = costAnalysisDao.selectCodeMapper();
		System.err.println(energyTypeCode);
		System.err.println(Start+","+End);
		for(int j = 0; j<energyTypeCode.size(); j++){
			data.add(timeAdd(machineDao.selectMonitorMedia(energyTypeCode.get(j),Start,End)));
		}
		System.out.println("monthBySetTime="+data);
		return data;
	}
	
	public List<String> hcMonth(){
		List<String> hcmonth = new ArrayList<>();
		for(int i =1;i<11;i++){
			hcmonth.add(String.valueOf(i*3));
		}
		return hcmonth;
	}
	
	/** 每年，任意能源的消耗总量
	 * 按月分段,1月,返回时间段数组
	 */
	public List<Double> year(String object,String time) throws ParseException{
		List<Date> year = timeSecrionYear(time);
		List<Double> data = new ArrayList<>();
		char[] num = object.toCharArray();
		if(Character.isDigit(num[0])){
			for(int i = 0;i<24;i++){
				if(i%2==0){
					data.add(timeAdd(machineDao.selectMonitorMedia(object,year.get(i),year.get(i+1))));
				}
			}
		}else{
			for(int i = 0;i<24;i++){
				if(i%2==0){
					data.add(timeAdd(machineDao.selectName(object, year.get(i), year.get(i+1))));
					System.out.println(year.get(i)+":"+year.get(i+1));
				}
			}
		}
		System.out.println("year="+data);
		return data;
	}
	public List<String> hcYear(){
		List<String> hcyear = new ArrayList<>();
		hcyear.add("Jan");
		hcyear.add("Feb");
		hcyear.add("Mar");
		hcyear.add("Apr");
		hcyear.add("May");
		hcyear.add("Jun");
		hcyear.add("Jul");
		hcyear.add("Aug");
		hcyear.add("Sep");
		hcyear.add("Oct");
		hcyear.add("Nov");
		hcyear.add("Dec");
		return hcyear;
	}
	// 时间刻度
	public List<Date> timeSectionDay(String time) throws ParseException{
		List<Date> timesection = new ArrayList<>();
		for(int i = 0 ; i < 5; i++){
			if(i==0){
				timesection.add(dateFormat.parse(time+" "+ "0" + String.valueOf(i*6) +":00:00"));
			}else if(i>0 && i<4){
				timesection.add(dateFormat.parse(time+" "+ String.valueOf(i*6) +":00:00"));
			}else if(i==4){
				timesection.add(dateFormat.parse(time+" "+"23:59:59"));
			}
		}
		for (Date date : timesection) {
			System.out.println(date);
		}
		return timesection;
	}
	
	public List<Date> timeSecrionMouth(String time) throws ParseException{
		System.out.println(time);
		List<Date> sum = new ArrayList<>();
		for(int i = 0 ; i < 11 ; i++){
			if(i < 4){
				sum.add(dateFormat.parse(time + "-0" + String.valueOf(i*3) + " " + "00:00:00"));
				sum.add(dateFormat.parse(time + "-0" + String.valueOf((i+1)*3) + " " + "00:00:00"));
			}else if( i >= 4 && i < 11){
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i*3)+ " "  + "00:00:00"));
				sum.add(dateFormat.parse(time + "-" + String.valueOf((i+1)*3)+ " "  + "00:00:00"));
			}else if(i == 11){
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i*3)+ " "  + "00:00:00"));
				sum.add(dateFormat.parse(time + "-" + String.valueOf((i+1)*3)+ " "  + "00:00:00"));
			}
		}
		System.out.println("timeSecrionMouth="+sum);
		return sum;
	}
	
	public List<Date> timeSecrionYear(String time) throws ParseException{
		List<Date> sum = new ArrayList<>();
		for(int i = 1 ; i < 13 ; i++){
			if(i < 10){
				sum.add(dateFormat.parse(time + "-" + "0" + String.valueOf(i) + "-00" + " " + "00:00:00"));
				sum.add(dateFormat.parse(time + "-" + "0" + String.valueOf(i) + "-31" + " " + "23:59:59"));
			}else if(i == 10){
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i) + "-00" + " " + "00:00:00"));
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i) + "-31" + " " + "23:59:59"));
			}else if(i == 11){
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i) + "-00" + " " + "00:00:00"));
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i) + "-31" + " " + "23:59:59"));
			}else if(i == 12){
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i) + "-00" + " " + "00:00:00"));
				sum.add(dateFormat.parse(time + "-" + String.valueOf(i) + "-31" + " " + "23:59:59"));
			}
		}
		for (Date date : sum) {
			System.out.println(date);
		}
		return sum;
	}
	public List<Double> selectAllDataByEnergySer(String time) throws ParseException{
		List<String> code = costAnalysisDao.selectCodeMapper();
		System.out.println(code);
		List<Double> sum = new ArrayList<>();
		Date time1 = null;
		Date time2 = null;
		if(time.length()<5){
			time1 = dateFormat.parse(time+"-01-01 00:00:00");
			time2 = dateFormat.parse(time+"-12-31 23:59:59");
		} else if (time.length()<8) {
			time1 = dateFormat.parse(time+"-01 00:00:00");
			time2 = dateFormat.parse(time+"-"+reportSummaryService.time(time)+" 23:59:59");	//当月的最大天数。
		}
		for(int i = 0; i<code.size(); i++){
			sum.add(timeAdd(machineDao.selectAllDataByEnergy(time1,time2,code.get(i))));
		}
		System.out.println("selectAllDataByEnergySer= "+ sum);
		return sum;
	}
	
	public void addMachineData(Machine machine){
		machineDao.addMachineData(machine);
	}
}
